package marker.micro.login.controller;

import java.security.Principal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import marker.micro.login.entity.User;
import marker.micro.login.entity.User.RoleType;
import marker.micro.login.service.CustomUserDetailsService;

@RestController
public class UserController {

    private final RoleType DEFAULT = RoleType.ROLE_USER;
    
    @Autowired
    private CustomUserDetailsService customUsedrDetailService;
    /**
     * 
     * Description:
     * 试试看，
     * 应该与ResourceController的public Object getPrincipal()的返回值一样吧
     * 
     * Last Modified: <br>
     * 2017年5月27日 下午3:07:30
     */
    @RequestMapping("/current")
    public Principal user(Principal user,
	    @RequestHeader(value="Authorization") String authorization) {
	return user;
    }
    
    @PostMapping("/registry")
    public boolean defaultRegistry(String username,String password) {
	User user = User.create(username, password, DEFAULT);
	return customUsedrDetailService.registry(user);
    }
    
    @GetMapping("/all")
    public List<User> allList() {
	return customUsedrDetailService.getUsers();
    }

}
